// pages/login/login.js
//获取应用实例
const app = getApp()
const util = require('../../../utils/util.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    loadding : 1,
    unaddressList:[],
    timeRecord:"所有",
    timeRecord2:"",
    checkboxItems: [
      { id: '0', value: '近7天' },
      { id: '1', value: '近14天' },
      { id: '2', value: '近1个月' },
      { id: '3', value: '近2个月' },
      { id: '4', value: '近3个月' },
      { id: '5', value: '近半年' },
    ],
    checkboxItems2: [
      { id: '0', value: '已巡查' },
      { id: '1', value: '未巡查' }
    ]
  },
  onLoad: function(options) {
    console.log(options)
    let flag = options.flag
    wx.request({
      url: app.inspectUrl + '/WeChat/weChatSystem/getNFCInspectAddressCondition',
      method: 'POST',
      dataType: 'json',
      data: {
        schoolId : parseInt(app.globalData.departmentId),
        type : parseInt(flag),
        param: options.inspectPlanId
      },
      success: res => {
        console.log("res.data",res.data)
        let unaddressList = [] ;
        let count1 = 0
        for(let i = 0; i<res.data.length;i++){
          unaddressList.push({
            address : res.data[i].address,
            deviceName: res.data[i].deviceName,
            recentTime : res.data[i].recentTime
          })
        } 
        this.setData({
          unaddressList : unaddressList,
          loadding:0
        })
      }
    })
  },

  showSelect: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "ease",
      delay: 0
    })
    this.animation = animation
    animation.translateY(800).step()
    this.setData({
      animationSelect: animation.export(),
      showSelectStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationSelect: animation.export()
      })
    }.bind(this), 200)
  },
  // 隐藏遮罩层
  hideSelect: function (e) {
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "ease",
      delay: 0
    })
    this.animation = animation
    animation.translateY(800).step()
    this.setData({
      animationSelect: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationSelect: animation.export(),
        showSelectStatus: false
      })
    }.bind(this), 200)
  },

  timeChange: function (e) {
    let checkboxValues=null
    let checkboxItems = this.data.checkboxItems 
    let values = e.detail.value 
    for (var i = 0, len = checkboxItems.length; i < len; ++i) {
      if(checkboxItems[i].value==values[values.length-1]){
        checkboxItems[i].checked=true;
        checkboxValues = checkboxItems[i].value;
      }
      else{
        checkboxItems[i].checked = false;
      }
    }
    this.setData({ checkboxItems, checkboxValues })
  },
  statusChange: function (e) {
    let checkboxValues2=null
    let checkboxItems2 = this.data.checkboxItems2 
    let values = e.detail.value 
    for (var i = 0, len = checkboxItems2.length; i < len; ++i) {
      if(checkboxItems2[i].value==values[values.length-1]){
        checkboxItems2[i].checked=true;
        checkboxValues2 = checkboxItems2[i].value;
      }
      else{
        checkboxItems2[i].checked = false;
      }
    }
    this.setData({ checkboxItems2, checkboxValues2 })
  },
  confirmSelect:function(e){
    let day 
    if((this.data.checkboxValues == undefined || this.data.checkboxValues == null)
      && (this.data.checkboxValues2 == undefined || this.data.checkboxValues2 == null)){
      wx.showToast({
        title: "请任意选择条件",
        image: "/images/warning.png"
      })
    }else{
      let unaddressList = [] ;
      if(this.data.checkboxValues == "近7天"){
        day = 8
      }else if(this.data.checkboxValues == "近14天"){
        day = 15
      }else if(this.data.checkboxValues == "近1个月"){
        day = 32
      }else if(this.data.checkboxValues == "近2个月"){
        day = 63
      }else if(this.data.checkboxValues == "近3个月"){
        day = 94
      }else if(this.data.checkboxValues == "近半年"){
        day = 184
      }else{
        day = 357
      }
      this.setData({
        loadding : 1
      })
      wx.request({
        url: app.inspectUrl + '/WeChat/weChatSystem/getNFCInspectConditionQuery',
        method: 'POST',
        dataType: 'json',
        data: {
          schoolId : parseInt(app.globalData.departmentId),
          datetime : parseInt(day)
        },
        success: res => {
          console.log(res.data)
          if(res.data != null){
            if(this.data.checkboxValues2 == undefined || this.data.checkboxValues2 == null){
              for(let i = 0; i<res.data.length;i++){
                unaddressList.push({
                  address : res.data[i].address,
                  deviceName: res.data[i].deviceName,
                  recentTime : res.data[i].recentTime
                })
              } 
              this.setData({
                timeRecord2 : "" ,
              })
            }else{
              if(this.data.checkboxValues2 == "未巡查"){
                for(let i = 0; i<res.data.length;i++){
                  if(res.data[i].recentTime == "无记录"){
                    unaddressList.push({
                      address : res.data[i].address,
                      deviceName: res.data[i].deviceName,
                      recentTime : res.data[i].recentTime
                    })
                  }
                }
              }else{
                for(let i = 0; i<res.data.length;i++){
                  if(res.data[i].recentTime != "无记录"){
                    unaddressList.push({
                      address : res.data[i].address,
                      deviceName: res.data[i].deviceName,
                      recentTime : res.data[i].recentTime
                    })
                  }
                }
              }
              this.setData({
                timeRecord2 : this.data.checkboxValues2 ,
              })
            }
            if(this.data.checkboxValues == null || this.data.checkboxValues == undefined){
              this.setData({
                timeRecord : "所有" ,
              })
            }else{
              this.setData({
                timeRecord : this.data.checkboxValues ,
              })
            }
            this.setData({
              unaddressList : unaddressList,
              loadding:0
            })
            wx.showToast({
              title: '搜索成功！',
              icon: 'none',
              duration: 1000
            })
            this.hideSelect() ;
          }else{
            wx.showToast({
              title: "搜索失败",
              image: "/images/error.png"
            })
          }
        }
      })
    }
  }



 

  
})

